#include "Rectangle.h"
#include <math.h>

Rectangle2::Rectangle2(float x, float y, float width, float height)
{
	m_topLeft.x = x;
	m_topLeft.y = y;
	m_size.x = width;
	m_size.y = height;
}

Rectangle2::Rectangle2(const Vector2D& topLeft, const Vector2D& btmRight)
{
	m_topLeft = topLeft;
	m_size = Vector2D::Substract(btmRight, topLeft);
}

bool Rectangle2::Contains(float x, float y) const
{
    return  (x + m_size.x >= m_topLeft.x && x <= m_topLeft.x + m_size.x)
            &&
            (y + m_size.y >= m_topLeft.y && y <= m_topLeft.y + m_size.y);

}

bool Rectangle2::Contains(const Vector2D& point) const
{
	return	(point.x + m_size.x >= m_topLeft.x && point.x <= m_topLeft.x + m_size.x)
			&& 
			(point.y + m_size.y >= m_topLeft.y && point.y <= m_topLeft.y + m_size.y);
}

float Rectangle2::GetX() const
{
	return m_topLeft.x;
}

float Rectangle2::GetY() const
{
	return m_topLeft.y;
}

float Rectangle2::GetWidth() const
{
	return m_size.x;
}

float Rectangle2::GetHeight() const
{
	return m_size.x;
}

void Rectangle2::SetPosition(const Vector2D& vect)
{
	m_topLeft.x = vect.x;
	m_topLeft.y = vect.y;
}

void Rectangle2::SetPosition(float x, float y)
{
	m_topLeft.x = x;
	m_topLeft.y = y;
}

void Rectangle2::MoveBy(const Vector2D& vect)
{
	m_topLeft.x += vect.x;
	m_topLeft.y += vect.y;
}

void Rectangle2::MoveBy(float x, float y)
{
	m_topLeft.x += x;
	m_topLeft.y += y;
}

bool Rectangle2::CollidesWith(const Rectangle2& rect) const
{
	bool hit = false;
	float length;
	float heigth;

	// affecte la difference positive
	length = m_topLeft.x > rect.GetX()
			?
				m_topLeft.x - rect.GetX()
			:
				rect.GetX() - m_topLeft.x;

	// affecte la difference positive
	heigth = m_topLeft.y > rect.GetY()
			?
				m_topLeft.y - rect.GetY()
			:
				rect.GetY() - m_topLeft.y;

	return	(2 * heigth <= rect.GetHeight() - m_size.y)
			&&
			(2 * length <= rect.GetWidth() - m_size.x);

}

